package com.ipcoder.TCP通信第八题;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;

public class Serverc implements Runnable {
    private Socket sk;

    public Serverc(Socket sk) {
        this.sk = sk;
    }

    public Serverc() {
    }

    @Override
    public void run() {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(sk.getInputStream()));
            int count = 0;
            File file = new File("D:\\黑马视频\\file\\server\\Copy" + count + ".txt");
            while (file.exists()) {
                count++;
                file = new File("D:\\黑马视频\\file\\server\\Copy" + count + ".txt");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            System.out.println("接收文件完成!");
            BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
            bwServer.write("上传文件成功");
            bwServer.newLine();
            bwServer.flush();
            bw.close();
            bwServer.close();
            br.close();
            sk.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Test {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9999);
        while (true) {
            Socket sk = ss.accept();
            new Thread(new Serverc(sk)).start();
        }

    }
}
